clear; close all; clc; 

load ../data/music_dataset.mat
addpath 'CV/' 'DT/' 'Lyrics_Kernel/' 'Boosting/'

[Xt_lyrics] = make_lyrics_sparse(train, vocab);
[Xq_lyrics] = make_lyrics_sparse(quiz, vocab);

Yt = zeros(numel(train), 1);
for i=1:numel(train)
    Yt(i) = genre_class(train(i).genre);
end

Xt_audio = make_audio(train);
Xq_audio = make_audio(quiz);

%%
Xt_lyrics = stemmer(Xt_lyrics,vocab);
Xq_lyrics = stemmer(Xq_lyrics,vocab);
fprintf('Stemming Complete \n');

% Compute word frequencies 
Xt_word_count = sum(Xt_lyrics); 

% Cut out infrequent word bins 
thresh = 5; 
Xt_lyrics = Xt_lyrics(:, Xt_word_count >=thresh); 

%% Train Binary Adaboost
% tic; 
% T = 5; 
% depth_limit = 3; 
% 
% K = numel( unique( Yt ) ); 
% for i = 1:K
%     Yt_binary = double(Yt == i); 
%     boost_struct.num2str(i) = adaboost_train(Xt_audio, Yt_binary, T, depth_limit); 
% end 

%% Train Adaboost 

tic;
T = 10; 
depth_limit = 6; 

% clip = 4000;
% ind = randperm(clip);
% x = xt_lyrics(ind,:);
% y = yt(ind,:);

% train adaboost for T rounds with depth_limt DT
boost = adaboost_train(Xt_audio, Yt, T, depth_limit); 
toc 

% Plot useful stuff 
f1 = figure(1); clf; hold on; grid on; 
plot(1:T, boost.rank_error, 'LineWidth', 2); 
a1 = get(f1, 'CurrentAxes');
set(a1, 'YLim', [0, 1])
xlabel('Number of Iterations', 'FontSize', 18); 
ylabel('Rank Error', 'FontSize', 18); 
hold off; 

figure(2); clf; hold on; grid on; 
plot(1:T, boost.err, 'LineWidth', 2); 
xlabel('Number of Iterations', 'FontSize', 18); 
ylabel('Weighted Error at T', 'FontSize', 18); 
hold off; 

%% Test Adaboost on quiz set 
test_err = adaboot_test(boost, Xt_quiz);  
save dt